<?php 

// Gets all users
$users = array();
$users = getUsers($_SESSION['user_id']);

// Get users roles
$user_roles = array();
for($i = 7; $i < count($users); $i++){
	array_push($user_roles, getRoleName($users[$i]));
	$i = $i + 7;
}

// Title
$output  = "<h1>Usercenter</h1>";
$output .= "<form action='#' method='post'>";

// Actionhandler
if(isset($_POST['delete'])){
	if(isset($_POST['sel_user'])){
		deleteUser($_POST['sel_user']);
		$output .= "<font color=green>User deleted</font>";
	}else{
		$output .= "<font color=red>Please select a user</font>";
	}
}else if(isset($_POST['edit'])){
	if(isset($_POST['sel_user'])){
		$_SESSION['sel_user_id'] = $_POST['sel_user'];
		
		// User information
		$user_id  = $_SESSION['sel_user_id'];
		$name     = getUserInformation($user_id, "name");
		$surname  = getUserInformation($user_id, "surname");
		$street   = getUserInformation($user_id, "street");
		$zip      = getUserInformation($user_id, "ZIP");
		$country  = getUserInformation($user_id, "country");
		$username = getUserInformation($user_id, "username");
		$role_id  = getUserInformation($user_id, "role_role_id");
		$rolename = getRoleName($role_id);
		$allRoles = getRoles();
		
		// Creates Role drop down
		$dd_roles = "<select name=userrole> ";
		for($i = 0; $i < count($allRoles); $i++){
			if($allRoles[$i]==$rolename){
				$dd_roles .= "<option value=$allRoles[$i] selected>$allRoles[$i]</option>";
			}else{
				$dd_roles .= "<option value=$allRoles[$i]>$allRoles[$i]</option>";
			}
		}
		$dd_roles .= "</select>";
		
		// Chart of all users
		$output .= "<table width=80%>";
		$output .= "<tr>";
		$output .= "<th>Userid</th>";
		$output .= "<th>Name</th>";
		$output .= "<th>Surname</th>";
		$output .= "<th>Street</th>";
		$output .= "<th>ZIP</th>";
		$output .= "<th>Country</th>";
		$output .= "<th>Username</th>";
		$output .= "<th>Userole</th>";
		$output .= "<th></th>";
		$output .="</tr>";
		$output .= "<tr>";
		$output .= "<td>$user_id</td>";
		$output .= "<td><input type=text name=name value=$name size=15/></td>";
		$output .= "<td><input type=text name=surname value=$surname size=15 /></td>";
		$output .= "<td><input type=text name=street value=$street size=15 /></td>";
		$output .= "<td><input type=text name=zip value=$zip size=20 /></td>";
		$output .= "<td><input type=text name=country value=$country size=15 /></td>";
		$output .= "<td>$username</td>";
		$output .= "<td>$dd_roles</td>";
		$output .= "<td><input type=submit name=update value='Update' /></td>";
		$output .="</tr>";
		$output .="</table>";
		$output .="<br>";
	}else{
		$output .= "<font color=red>Please select a user</font>";
	}
}else if(isset($_POST['update'])){
	$user_information    = array();
	$user_information[0] = $_POST['name'];
	$user_information[1] = $_POST['surname'];
	$user_information[2] = $_POST['street'];
	$user_information[3] = $_POST['zip'];
	$user_information[4] = $_POST['country'];
	$user_information[5] = $_POST['userrole'];
	
	editUser($user_information, $_SESSION['sel_user_id']);
	$output .= "<font color=green>User updated</font>";
}

// Chart of all users
$output .= "<table width=80%>";
$output .= "<tr>";
$output .= "<th>Userid</th>";
$output .= "<th>Name</th>";
$output .= "<th>Surname</th>";
$output .= "<th>Street</th>";
$output .= "<th>ZIP</th>";
$output .= "<th>Country</th>";
$output .= "<th>Username</th>";
$output .= "<th>Userole</th>";
$output .= "<th></th>";
$output .="</tr><p>";
$j = 0;
for($i = 0; $i < count($users); $i++){
	$output .= "<tr>";
	$output .= "<td>".$users[$i]."</td>";
	$output .= "<td>".$users[$i+2]."</td>";
	$output .= "<td>".$users[$i+1]."</td>";
	$output .= "<td>".$users[$i+3]."</td>";
	$output .= "<td>".$users[$i+4]."</td>";
	$output .= "<td>".$users[$i+5]."</td>";
	$output .= "<td>".$users[$i+6]."</td>";
	$output .= "<td>".$user_roles[$j]."</td>";
	if($_SESSION['user_id'] != $users[$i]){
		$output .= "<td><input type='radio' name='sel_user' value='".$users[$i]."' /></td>";
	}
	$output .="</tr>";
	$i = $i + 7;
	$j++;
}

// Edit and Delete user
$output .= "<tr>";
$output .= "<td><input type=submit name=delete value='Delete User' /></td>";
$output .= "<td><input type=submit name=edit value='Edit User' /></td>";
$output .= "<td><input type=submit name=refresh value='Refresh' /></td>";
$output .="</tr>";
$output .="</p></table></form>";

//Output
echo $output;
?>